<template>
    <div>
        <Modal
         title="更改租户"
         :value="value"
         @on-ok="changeTenant"
         @on-visible-change="visibleChange"
        >
             <Input placeholder="请输入租户名" v-model="changedTenancyName"></Input>
             <div v-if="!changedTenancyName" style="margin-top:10px">注意：留空以切换到HOST</div>
             <div slot="footer">
                <Button icon="md-close" @click="cancel">取消</Button>
                <Button icon="md-checkmark" @click="changeTenant" type="primary">确定</Button>
             </div>
        </Modal>
    </div>
</template>
<script lang="ts">
import { Component, Vue,Inject, Prop,Watch } from 'vue-property-decorator';
import Util from '@/lib/util'
import AbpBase from '@/lib/abpbase'
@Component
export default class TenantSwitch extends AbpBase{
    @Prop({default:false}) value:boolean
    changedTenancyName:string='';
    get tenant(){
        return this.$store.state.session.tenant;
    }
    cancel(){
        this.$emit('input',false);
    }
    visibleChange(value:boolean){
        if(!value){
            this.$emit('input',value);
        }else{
            this.changedTenancyName=this.$store.state.session.tenant?this.$store.state.session.tenant.name:''
        }
    }
    async changeTenant(){
        if (!this.changedTenancyName) {
            Util.abp.multiTenancy.setTenantIdCookie(undefined);;
            location.reload();
            return;
        }else{
                let tenant=await this.$store.dispatch({
                    type:'account/isTenantAvailable',
                    data:{tenancyName:this.changedTenancyName}
                })
                switch(tenant.state){
                    case 1:
                        Util.abp.multiTenancy.setTenantIdCookie(tenant.tenantId);
                        location.reload();
                        return;
                    case 2:
                        this.$Message.error({background:true,content:'租户' + this.changedTenancyName  + ' 未激活',duration:this.$config.duration});
                        break;
                    case 3:
                        this.$Message.error({background:true,content:'租户 ' + this.changedTenancyName + ' 不存在',duration:this.$config.duration});
                        break;
                }
            }
    }
}
</script>

